import { Controller, Get, Post, Put, Delete, Body, Param, NotFoundException, BadRequestException, UploadedFile, UseInterceptors } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { WorkflowManagementService } from '../services/workflow-management.service';
import { CreateWorkflowDto, UpdateWorkflowDto } from '../dto/workflow.dto';
import { FileInterceptor } from '@nestjs/platform-express';

@ApiTags('workflow')
@Controller('api/workflow')
export class WorkflowManagementController {
    constructor(private workflowManagementService: WorkflowManagementService) { }

    @Get()
    @ApiOperation({ summary: '获取所有工作流' })
    async getAllWorkflows() {
        return this.workflowManagementService.getAllWorkflows();
    }

    @Get(':id')
    @ApiOperation({ summary: '获取单个工作流' })
    async getWorkflow(@Param('id') id: string) {
        const workflow = await this.workflowManagementService.getWorkflow(id);
        if (!workflow) {
            throw new NotFoundException('工作流不存在');
        }
        return workflow;
    }

    @Post()
    @ApiOperation({ summary: '创建新工作流' })
    async createWorkflow(@Body() workflowData: CreateWorkflowDto) {
        try {
            const result = await this.workflowManagementService.createWorkflow(workflowData);
            return {
                success: true,
                data: result
            };
        } catch (error) {
            if (error instanceof BadRequestException) {
                throw error;
            }
            throw new BadRequestException('创建工作流失败');
        }
    }

    @Put(':id')
    @ApiOperation({ summary: '更新工作流' })
    async updateWorkflow(
        @Param('id') id: string,
        @Body() workflowData: UpdateWorkflowDto
    ) {
        const result = await this.workflowManagementService.updateWorkflow(id, workflowData);
        return {
            success: true,
            data: result
        };
    }

    @Delete(':id')
    @ApiOperation({ summary: '删除工作流' })
    async deleteWorkflow(@Param('id') id: string) {
        await this.workflowManagementService.deleteWorkflow(id);
        return { success: true };
    }

    @Get(':id/export')
    @ApiOperation({ summary: '导出工作流' })
    async exportWorkflow(@Param('id') id: string) {
        try {
            const workflow = await this.workflowManagementService.getWorkflow(id);
            if (!workflow) {
                throw new NotFoundException('工作流不存在');
            }

            // 移除不需要导出的字段
            const exportData = {
                ...workflow.data.toObject(),
                _id: undefined,
                __v: undefined,
                createTime: undefined,
                updateTime: undefined,
                stats: undefined
            };

            return exportData;
        } catch (error) {
            throw new BadRequestException('导出工作流失败');
        }
    }

    @Post('import')
    @ApiOperation({ summary: '导入工作流' })
    @UseInterceptors(FileInterceptor('file'))
    async importWorkflow(@UploadedFile() file: Express.Multer.File) {
        try {
            const workflowData = JSON.parse(file.buffer.toString());

            // 验证导入的数据
            await this.workflowManagementService.validateWorkflow(workflowData);

            // 创建新的工作流
            const workflow = await this.workflowManagementService.createWorkflow({
                ...workflowData,
                stats: { uses: 0, likes: 0, views: 0 }
            });

            return { data: workflow };
        } catch (error) {
            throw new BadRequestException('导入工作流失败');
        }
    }

    @Post(':id/like')
    @ApiOperation({ summary: '点赞工作流' })
    async likeWorkflow(@Param('id') id: string) {
        return this.workflowManagementService.likeWorkflow(id);
    }

    @Post(':id/generate')
    @ApiOperation({ summary: '生成图片' })
    async generateImage(
        @Param('id') id: string,
        @Body() params: Record<string, any>
    ) {
        return this.workflowManagementService.generateImage(id, params);
    }

    @Post(':id/test')
    @ApiOperation({ summary: '测试工作流' })
    async testWorkflow(@Param('id') id: string) {
        return this.workflowManagementService.testWorkflow(id);
    }
} 